/**
  ******************************************************************************
  * @file    download.c 
  * @author  
  * @version 
  * @date    
  * @brief   This file provides the software which allows to download an image 
  *          to internal Flash.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  */ 

/** 
  * @{
  */
/* Includes ------------------------------------------------------------------*/
#include "common.h"
#include "debug_uart.h"
#include "ymodem.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

uint8_t file_name[FILE_NAME_LENGTH];
uint8_t tab_1024[1024] =
{
    0
};

extern void mdelay(uint32_t ul_dly_ticks);

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Download a file via serial port
  * @param  None
  * @retval None
  */
int8_t download_via_serial(void)
{
  uint8_t Number[10] = "";
  int32_t Size = 0;
  
  debug_print("waiting for the file to be sent...\n\r");
  Size = Ymodem_Receive(&tab_1024[0]);
  if (Size == 0)
  {
    debug_print("\n\rsize=0\n\r");
    return 0;
  }
  if (Size > 0)
  {
    debug_print("programming completed successfully!\n\r");
    debug_print("name:%s",file_name);
    Int2Str(Number, Size);
    debug_print("file size:%d Bytes\r\n",Number);
    return Size;
  }
  else if (Size == -1)
  {
    debug_print("image size is higher than the allowed space memory!\n\r");
    return -1;
  }
  else if (Size == -2)
  {
    debug_print("verification failed!\n\r");
    return -2;
  }
  else if (Size == -3)
  {
    debug_print("aborted by user.\n\r");
    return -3;
  }
  else if(Size == -4)
  {
    debug_print("failed to receive the file!\n\r");
    return -4;
  }
  else
  {
    debug_print("wait time out\n\r");
    return -5;
  }


}

/**
  * @}
  */

/************************END OF FILE********************/
